package com.gy.smart.handle;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseBean;
import com.gy.smart.base.BaseFragment;
import com.gy.smart.handle.adapter.HandleThemeAdapter;
import com.gy.smart.handle.bean.HandleThemeBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.Urls;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 17/1/4
 * 说明：办事主题
 */
public class HandleThemeFragment extends BaseFragment {

    private GridView gridView;
    private HandleThemeAdapter handleThemeAdapter;
    private List<HandleThemeBean> datas,requestList;

    @Override
    protected View setView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_handle_theme,null);
        hidenTop();
        return view;
    }

    @Override
    protected void initView() {
        gridView = (GridView) _rootView.findViewById(R.id.fg_handle_theme_gridview);
    }

    @Override
    protected void initData() {
        datas = new ArrayList<>();
        requestList = new ArrayList<>();
        handleThemeAdapter = new HandleThemeAdapter(mContext,datas);
    }

    @Override
    protected void setListener() {
        super.setListener();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext,HandleListActivity.class);
                intent.putExtra("theme_id",datas.get(position).theme_id);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(getActivity());
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        gridView.setAdapter(handleThemeAdapter);
        getList();
    }

    private void getList(){
        showLoadDialog();
        RequestParams params = new RequestParams(Urls.url_handle_theme);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<HandleThemeBean>>(){}.getType();
                    requestList = gson.fromJson(result.data,type);
                    datas.addAll(requestList);
                    handleThemeAdapter.notifyDataSetChanged();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
